package com.cjjd.c.utils;

import android.annotation.SuppressLint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

@SuppressLint("NewApi")
public class DateTimeUtil {

    private static final String TAG = DateTimeUtil.class.getSimpleName();

    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    private static final SimpleDateFormat FORMAT2 = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
    private final static SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    private final static SimpleDateFormat dateFormater3 = new SimpleDateFormat("HH:mm", Locale.getDefault());
    private static final long MINUTE_AGO = 1 * 60 * 1000; // 1分钟以前
    private static final long HOUR_AGO = 1 * 60 * 60 * 1000; // 1小时以前
    private static final long DAY_AGO = 24 * 60 * 60 * 1000; // 24小时以前
    private static final long FIVE_DAY_AGO = 5 * 24 * 60 * 60 * 1000; // 5天以前
    private static final String HTML_FONT_STYLE_START = "<font color='#ffffff'><big><big>";
    private static final String HTML_FONT_STYLE_END = "</big></big></font>";
    public static SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy年M月d日", Locale.getDefault());

    /**
     * 返回多少时间 多少秒前 多少小时前
     *
     * @param time
     * @return
     */
    public static String formatTimestamp(long time) {
        long currentTime = System.currentTimeMillis();
        long serverTime = time * 1000;
        long intervalTime = currentTime - serverTime;
        if (intervalTime < MINUTE_AGO) {
            long l = (intervalTime % (1000 * 60)) / 1000;
            if (l > 0)
                return l + "秒前";
            else
                return "1秒前";
        } else if (intervalTime < HOUR_AGO) {
            return (intervalTime % (1000 * 60 * 60)) / (1000 * 60) + "分钟前";
        } else if (intervalTime < DAY_AGO) {
            return (intervalTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60) + "小时前";
        } else if (intervalTime < FIVE_DAY_AGO) {
            return intervalTime / (1000 * 60 * 60 * 24) + "天前";
        } else {
            return FORMAT.format(new Date(serverTime));
        }
    }

    /**
     * 获取年
     *
     * @param timestamp ：时间戳。1970年1月1日后的毫秒数
     * @return
     */
    public static int getYear(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.CHINA);
        currentCalendar.setTimeInMillis(timestamp);


        try {
            FileWriter fw = new FileWriter("000.txt");
            BufferedWriter bw = new BufferedWriter(fw);

            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

        return currentCalendar.get(Calendar.YEAR);
    }

    /**
     * 获取月
     *
     * @param timestamp ：时间戳。1970年1月1日后的毫秒数
     * @return
     */
    public static int getMonth(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.CHINA);
        currentCalendar.setTimeInMillis(timestamp);
        return currentCalendar.get(Calendar.MONTH);
    }

    /**
     * 获取几号，每月的第一天返回1
     *
     * @param timestamp ：时间戳。1970年1月1日后的毫秒数
     * @return
     */
    public static int getDayOfMonth(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.CHINA);
        currentCalendar.setTimeInMillis(timestamp);
        return currentCalendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 格式化日期
     *
     * @param template  ：格式。"yyyy-MM-dd HH:mm"等
     * @param timestamp ：时间戳。1970年1月1日后的毫秒数
     * @return
     */
    public static String format(String template, long timestamp) {
        SimpleDateFormat formate = new SimpleDateFormat(template, Locale.CHINA);
        return formate.format(new Date(timestamp));
    }


    /**
     * 格式化日期
     *
     * @param dateString ：符合 template1 的 string
     * @param template1  ：格式。"yyyy-MM-dd HH:mm"等
     * @param template2  ：格式。"yyyyMMdd"等
     * @return : 得到符合 template2 的字符串
     */
    public static String format(String dateString, String template1, String template2) {

        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(template1);
            SimpleDateFormat sdf2 = new SimpleDateFormat(template2);
            Date date = sdf1.parse(dateString);
            return sdf2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 格式化日期
     *
     * @param template  ：格式。"yyyy-MM-dd HH:mm"等
     * @param timestamp ：时间戳。1970年1月1日后的毫秒数
     * @return
     */
    public static String string2Format(String template, String timestamp) {
        SimpleDateFormat formate = new SimpleDateFormat(template, Locale.CHINA);
        return formate.format(new Date(Long.valueOf(timestamp)));
    }

    public static String format1(long timestamp) {
        SimpleDateFormat formate = new SimpleDateFormat("y年M月d日", Locale.CHINA);
        return formate.format(new Date(timestamp));
    }

    public static String format2(long timestamp) {
        SimpleDateFormat formate = new SimpleDateFormat("y年M月d日", Locale.CHINA);
        return formate.format(new Date(timestamp * 1000));
    }


    public static String formatNYR(String day) {

        SimpleDateFormat formate = new SimpleDateFormat("M月d日", Locale.CHINA);

        return formate.format(new Date(stringToMillionSeconds(day) * 1000));
    }

    public static String getNY(String str) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sim.parse(str);
            SimpleDateFormat str2 = new SimpleDateFormat("MM月dd日");//时间格式
            Date date = new Date(); //当前时间
            String string = str2.format(d);
            return string;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 计算两个时间的间隔天数
     *
     * @param timestamp1 毫秒数
     * @param timestamp2 毫秒数
     * @return
     */
    public static int getBetweenDays(long timestamp1, long timestamp2) {
        // 当前时间

        Calendar currentCalendar = Calendar.getInstance(Locale.CHINA);
        currentCalendar.setTimeInMillis(timestamp1);
        currentCalendar.set(Calendar.HOUR_OF_DAY, 0);
        currentCalendar.set(Calendar.MINUTE, 0);
        currentCalendar.set(Calendar.SECOND, 0);
        currentCalendar.set(Calendar.MILLISECOND, 0);

        // 预产期
        Calendar birthdayCalendar = Calendar.getInstance(Locale.CHINA);
        birthdayCalendar.setTimeInMillis(timestamp2);
        birthdayCalendar.set(Calendar.HOUR_OF_DAY, 0);
        birthdayCalendar.set(Calendar.MINUTE, 0);
        birthdayCalendar.set(Calendar.SECOND, 0);
        birthdayCalendar.set(Calendar.MILLISECOND, 0);

        long absTime = Math.abs(currentCalendar.getTimeInMillis() - birthdayCalendar.getTimeInMillis());

        int day = (int) (absTime / (24 * 60 * 60 * 1000L));

        return day;
    }


    /**
     * 计算两个时间的间隔天数
     */
    public static int getBetweenDays(String day1, String day2, String format) {
        // 当前时间


        int day = 0;
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(format);

            Date date1 = sdf1.parse(day1);
            Date date2 = sdf1.parse(day2);
            day = date2.compareTo(date1);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return day;
    }

    /**
     * 获取time时间到当前时间的小时数
     *
     * @param time
     * @param format    : 格式。"yyyy-MM-dd HH:mm"等
     * @return
     */
    public static int getHourBetweenTwoTimes(String time, String format) {

        int hours = 0;
        SimpleDateFormat sdf1 = new SimpleDateFormat(format);
        try {
            Date date = sdf1.parse(time);
            hours = (int) Math.abs(date.getTime() - getCurrentTime()) / (60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return hours;
    }


    /**
     * 大于2天显示完整时间 2天内显示昨天 HH:mm 1天内显示 HH:mm
     *
     * @param time 时间秒数
     * @return
     */
    public static String formatTimestampForNotice(long time) {
        long currentTime = System.currentTimeMillis();
        long serverTime = time * 1000;
        long intervalTime = currentTime - serverTime;
        if (intervalTime / DAY_AGO < 1) {
            return dateFormater3.format(new Date(serverTime));
        } else if (intervalTime / DAY_AGO < 2) {
            return "昨天 " + dateFormater3.format(new Date(serverTime));
        } else {
            return dateFormater.format(new Date(serverTime));
        }
    }

    public static String timestempToStringMore(String time) {
        return dateFormater.format(new Date(Long.parseLong(time) * 1000));
    }

    public static String timestempToStringMore2(String time) {
        return dateFormater.format(new Date(Long.parseLong(time)));
    }

    /**
     * 毫秒数转时间字符(2011-01-01格式)
     *
     * @param time
     * @return
     */
    public static String timestempToString(long time) {
        try {
            return FORMAT.format(new Date(time));
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * 比较是否是同一天
     *
     * @return true相同的一天 false不同的一天
     */
    public static boolean isSameDay(long time) {
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String sp_time = sf.format(time);
        String current_time = sf.format(timeMillis);

        return sp_time.equals(current_time);
    }

    public static boolean isSameDay(String remoteTime, long localTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String sp_time = sf.format(localTime);
        return sp_time.equals(remoteTime);
    }

    public static boolean isSameDay(long remoteTime, long localTime) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String r_time = sf.format(remoteTime);
        String l_time = sf.format(localTime);
        return l_time.equals(r_time);
    }


    public static long stringToMillionSeconds(String time) {
        long millionSeconds = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        try {
            LogUtil.i(TAG, "timeToMillionSeconds time[" + time + "]");
            millionSeconds = sdf.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }// 毫秒
        return millionSeconds / 1000;
    }

    /**
     * @param format // 格式
     * @param time   // 要格式的string的时间
     * @return // long值
     */
    public static long stringToMillionSeconds(String format, String time) {
        long millionSeconds = 0;

        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        try {
            LogUtil.e(TAG, "timeToMillionSeconds time[" + time + "]");
            millionSeconds = sdf.parse(time).getTime();
            return millionSeconds;
        } catch (ParseException e) {
            e.printStackTrace();
        }// 毫秒
        return millionSeconds;
    }


    /**
     * 返回当前日期毫秒数
     *
     * @return
     */
    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    /**
     * 格式化时间
     *
     * @return yyyy-MM-dd格式时间
     */
    public static String logTime(long millis) {
        return FORMAT.format(new Date(millis));
    }

    /**
     * 格式化时间
     *
     * @return yyyy-MM-dd HH-mm格式时间
     */
    public static String logTime2(long millis) {
        return dateFormater.format(new Date(millis));
    }

    /**
     * 获取毫秒数
     *
     * @param dateString yyyy-MM-dd
     * @return long mills
     */
    public static long getTimeMills(String dateString) {
        String date[] = dateString.split("-");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        try {
            calendar.set(Calendar.YEAR, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return calendar.getTimeInMillis();
    }

    /**
     * 获取英文月份和日
     *
     * @param timestamp 0 月
     *                  1 日
     * @return
     */
    public static String[] getMonthAndDay(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.US);
        currentCalendar.setTimeInMillis(timestamp);
        String month = currentCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
        int day = currentCalendar.get(Calendar.DAY_OF_MONTH);
        if (day < 10) {
            return new String[]{month, "0" + day + ""};
        }
        return new String[]{month, day + ""};
    }

    public static String[] getMonthAndDayLong(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.US);
        currentCalendar.setTimeInMillis(timestamp);
        String month = currentCalendar.getDisplayName(Calendar.MONTH,
                Calendar.LONG, Locale.US);
        int day = currentCalendar.get(Calendar.DAY_OF_MONTH);
        if (day < 10) {
            return new String[]{month, "0" + day + ""};
        }
        return new String[]{month, day + ""};
    }

    public static int getMonthAndD(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance(Locale.US);
        currentCalendar.setTimeInMillis(timestamp);
        int day = currentCalendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public static int getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static long getMonthFristDay() {
        Calendar cal = Calendar.getInstance();

        // SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
        // 当前月的第一天
        cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        // String beginTime1 = datef.format(beginTime) + " 00:00:00";

        return beginTime.getTime();
    }

}
